package main

import (
	"os"
	"strconv"
	"time"
)

const goEol string = "\n"

func main() {
	var intChan = make(chan int, 10)
	var exitChan = make(chan bool, 1)
	//var startTIme = time.Now().UnixNano()
	go writeContent(intChan)
	go readChan(intChan, exitChan)
	for {
		_, ok := <-exitChan
		if !ok {
			break
		}
	}
	//var endTime = time.Now().UnixNano()
	//fmt.Println(endTime - startTIme)
}

func writeContent(intChan chan int) {
	for index := 0; index < 10; index++ {
		filePath := "./gogogo" + strconv.FormatInt(int64(index), 10) + ".txt"
		file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
		defer file.Close()
		var NanoSeconds = int64(time.Now().UnixNano())
		var timeStr = strconv.FormatInt(NanoSeconds, 10)
		intChan <- index
		file.Write([]byte(timeStr + goEol))
	}
	close(intChan)
}

func readChan(intChan chan int, exitChan chan bool) {
	for index := 0; index < 10; index++ {
		<-intChan
	}
	if len(exitChan) < 1 {
		exitChan <- true
	}
	close(exitChan)
}
